<?php
// 引入配置文件
require_once 'resource/config.php';
require_once 'resource/sql_commands.php';

// 创建连接
$conn = getDbConnection();

// 查询数据库中的表
$sql = "SHOW TABLES";
$result = $conn->query($sql);

$tables = array();
if ($result->num_rows > 0) {
    while($row = $result->fetch_assoc()) {
        $tables[] = $row['Tables_in_' . getCurrentDatabaseName()];
    }
}

$conn->close();
?>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title><?php echo WEB_NAME; ?></title>
    <style>
        body { font-family: Arial, sans-serif; max-width: 800px; margin: 0 auto; padding: 20px; }
        h1 { color: #333; }
        .table-list { margin-top: 20px; }
        .table-item { padding: 10px; margin-bottom: 10px; background-color: #f9f9f9; border-radius: 5px; }
        .table-name { font-weight: bold; }
        .create-table { margin-top: 30px; padding: 20px; background-color: #e9e9e9; border-radius: 5px; }
        button { padding: 8px 16px; background-color: #4CAF50; color: white; border: none; border-radius: 4px; cursor: pointer; }
        button:hover { background-color: #45a049; }
    </style>
</head>
<body>
    <h1>数据库自动生成管理系统</h1>
    <p>数据库: <?php echo getCurrentDatabaseName(); ?></p>

    <div class="table-list">
        <h2>数据库表 (<?php echo count($tables); ?>)</h2>
        <?php if (count($tables) > 0): ?>
            <ul>
                <?php foreach ($tables as $table): ?>
                    <li class="table-item">
                        <span class="table-name"><?php echo $table; ?></span>
                        <?php 
                        // 获取中文表名
                        $conn = getDbConnection();
                        $sql = "SELECT TABLE_COMMENT FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = '$table'";
                        $result = $conn->query($sql);
                        if ($result && $result->num_rows > 0) {
                            $row = $result->fetch_assoc();
                            $comment = $row['TABLE_COMMENT'];
                            if (!empty($comment)) {
                                echo " <span class=\"table-comment\">($comment)</span>";
                            }
                        }
                        $conn->close();
                        ?>
                    </li>
                <?php endforeach; ?>
            </ul>
        <?php else: ?>
            <p>数据库中没有表。</p>
        <?php endif; ?>
    </div>

    <div class="create-table">
        <h2>生成网站</h2>
        <p>点击按钮根据现有表结构生成完整网站:</p>
        <button onclick="location.href='generate_site.php'">生成网站</button>
    </div>
</body>
</html>